from time import sleep

from selenium.webdriver.common.by import By
from frp_page_object.base_page import BasePage, logger


class MenuPage(BasePage):
    loc_top_company = (By.XPATH, '//*[@id="orgSelect"]//input[@type="text"]')
    loc_sales_item = (By.XPATH, '//*[@id="sidebarMenu"]//p[contains(text(),"销售")]/..')
    loc_retail_order_item = (By.XPATH, '//ul/span[2]//*[contains(text(),"零售单")]')

    def click_retail_order(self):
        self.select_top_company('江苏苏州市-葛永理')
        sleep(1)
        self.click(self.loc_sales_item, 10)
        self.click_wait(self.loc_retail_order_item)

    def click_samples_order(self):
        self.select_top_company('江苏苏州市-葛永理')
        sleep(1)
        self.click(self.loc_sales_item, 10)
        self.click_wait((By.XPATH, '//*[text()="上下样申请"]'))

    def select_top_company(self, company_name):
        self.input(self.loc_top_company, company_name)
        try:
            self.click((By.XPATH, f'//*[@id="orgSelect"]//li[contains(text(),"{company_name}")]'), 5)
        except Exception as e:
            logger.error(f'找不到经销商{company_name}')
